package com.info.zhiduoduo.core.service;

import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.info.zhiduoduo.alipay.constant.AlipayIdentityEnum;
import com.info.zhiduoduo.alipay.constant.AlipayPrincipalEnum;
import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.repository.entity.maker.MakerAlipayAccountOrder;
import com.info.zhiduoduo.repository.entity.maker.MakerAlipayPrincipal;
import com.info.zhiduoduo.repository.entity.maker.MakerAlipayPrincipalOrder;

import org.apache.logging.log4j.util.BiConsumer;

import java.math.BigDecimal;
import java.util.function.Consumer;

import javax.servlet.http.HttpServletResponse;

public interface CoreMakerAlipayService {

	AlipayPrincipalEnum getISVPrincipal();

	MakerAlipayPrincipal queryZddPrincipalAccount(int principalId);

	/**
	 * 从 平台委托人的主账户记账本转账到创客的记账本.
	 *
	 * @param principalId  委托人id
	 * @param targetUserId 目标受托人id(创客id)
	 * @param amount       金额
	 * @return
	 */
	AlipayFundTransUniTransferResponse transfer2Entrustee(int principalId, int targetUserId, BigDecimal amount, int projectId, int projectType);

	MakerAlipayPrincipalOrder createPrincipalOrder(int isvId, String orderNo, MakerAlipayPrincipal alipayPrincipal);

	/**
	 * 解冻之前被冻结的委托人主账户余额
	 *
	 * @param principalId 0, 取当前 平台配置的主体
	 * @param amount      被冻结的金额， 正数
	 * @return
	 */
	boolean unfrozonPrincipalAmount(int principalId, BigDecimal amount);

	/**
	 * 从支付主体的记账本转账给个人支付宝账号/个人银行卡
	 *
	 * @param orderNo        平台的交易编号
	 * @param projectId     订单项目id-  项目id或智众包项目id
	 * @param projectType   项目类型，参考{@link ProjectType}
	 * @param principalId   支付主体id
	 * @param amount        代付金额,单位元,2位小数
	 * @param orderTitle    订单标题
	 * @param identityEnum  收款人身份标识，支持支付宝账号或者参考个人银行卡 {@link AlipayIdentityEnum}
	 * @param payeeIdentity 支付宝账号或银行卡号
	 * @param payeeName     收款人姓名或账户名
	 * @param doAfterPay    支付请求发送成功后的后续步骤
	 * @return
	 */
	AlipayFundTransUniTransferResponse principalPay(String orderNo, int projectId, int projectType, int principalId, BigDecimal amount,
	                                                String orderTitle, AlipayIdentityEnum identityEnum, String payeeIdentity,
	                                                String payeeName,
	                                                BiConsumer<MakerAlipayPrincipalOrder, AlipayFundTransUniTransferResponse> doAfterPay);

	/**
	 * 处理支付主体的订单查询结果
	 *
	 * @param order           支付主体订单
	 * @param response        查询结果
	 * @param successFunction 支付成功的后续步骤
	 */
	void handlePrincipalOrderQueryResponse(MakerAlipayPrincipalOrder order, AlipayFundTransCommonQueryResponse response,
	                                       Consumer<MakerAlipayPrincipalOrder> successFunction);

	/**
	 * 支付主体支付成功后，记录支付主体的账户流水
	 *
	 * @param order
	 */
	void recordPrincipalPayFlow(MakerAlipayPrincipalOrder order);

	void downloadEreceiptAndSave(MakerAlipayAccountOrder order) throws Exception;

	void downloadEreceiptFile(HttpServletResponse response, int itemId);

	void downloadEreceiptZipFile(HttpServletResponse response, int projectId);
}
